<template>
	<view class="addBook">
		<view class="coverBox">
			<image class="coverBg" :src="formData.image || '../../static/image/rqb.png'" mode="aspectFill"></image>
			<view class="uploadBth" @click="getImage">
				<image src="../../static/image/upload.png" mode="aspectFit"></image>
				<text>上传图片</text>
			</view>
		</view>
		<u--form :model="formData" :rules="rules" ref="uForm" labelPosition="top" labelWidth="auto">
			<template>
				<u-form-item label="人情簿名称" prop="name">
					<u--input v-model="formData.name" placeholder="请填写人情簿名称" clearable
						:customStyle="{border:'2rpx solid #ddd',background: '#f9f9f9',marginTop: '20rpx'}"></u--input>
				</u-form-item>
			</template>
			<u-form-item label="人情簿描述" prop="remark">
				<u--textarea v-model="formData.remark" placeholder="请填写人情簿描述" clearable
					:customStyle="{border:'2rpx solid #ddd',background: '#f9f9f9', marginTop: '20rpx'}"></u--textarea>
			</u-form-item>
		</u--form>
		<view class="btn" @click="submit">保存</view>
	</view>
</template>

<script>
	import {
		baseUrl
	} from '../../static/utils/env'
	import {
		post
	} from '../../static/utils/request'
	import {
		Giftbook,
		Upload
	} from '../../static/utils/service'
	import {
		Routes,
		Toast,
		UniBaseApi
	} from '../../static/utils/utils'
	export default {
		data() {
			return {
				formData: {
					name: '',
					remark: '',
					image: ''
				},
				rules: {
					'name': {
						required: true,
						message: '请填写人情簿名称',
						trigger: ['blur', 'change']
					}
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		onLoad() {
			this.eventChannel = this.getOpenerEventChannel()
		},
		methods: {
			async getImage() {
				const urls = await UniBaseApi.chooseImage(1)
				const res = await UniBaseApi.uploadImage(`${baseUrl}/${Upload.upload}`, urls)
				this.formData.image = res.uri
			},
			submit() {
				this.$refs.uForm.validate().then(async () => {
					const {
						code,
						msg
					} = await post(Giftbook.add, this.formData, '提交中')
					if (code === 1) {
						this.eventChannel.emit('update')
						Routes.back()
					} else Toast.none(msg)
				}).catch(err => {
					Toast.error('请完善信息')
				})
			}
		}
	}
</script>

<style lang="scss">
	.addBook {
		padding: 30rpx;

		.coverBox {
			height: 476rpx;
			position: relative;

			.coverBg {
				width: 100%;
				height: 100%;
				border-radius: 20rpx 20rpx 0 0;
			}

			.uploadBth {
				position: absolute;
				right: 30rpx;
				bottom: 30rpx;
				width: 210rpx;
				height: 70rpx;
				border-radius: 70rpx;
				background-color: #f05948;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;

				image {
					width: 30rpx;
					height: 30rpx;
				}

				text {
					margin-left: 10rpx;
					font-size: 28rpx;
				}
			}
		}

		.btn {
			margin-top: 70rpx;
			height: 80rpx;
			border-radius: 80rpx;
			background-color: #f05948;
			text-align: center;
			line-height: 80rpx;
			color: #fff;
		}
	}
</style>